package Model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import Dao.EmployeeManageDao;
import Entity.Employee;

public class EmployeeManageModel extends AbstractTableModel{
	private List<Employee> employeeList = new Vector<Employee>();
	private String[] header = { "STT", "EmpNumber", "Password", "EmpName", "Roles", "Address", "Phone", "Email", "WorkExp", "Birthday", "CurrentLocation", "CurrentDepartment", "CurrentProject", "Allowance" };

	public EmployeeManageModel() throws IOException, ClassNotFoundException,
			SQLException {
	}

	public int getColumnCount() {

		return header.length;
	}

	public int getRowCount() {
		return employeeList.isEmpty() == true ? 0 : employeeList.size();
	}

	/*public boolean isCellEditable(int rowIndex, int columnIndex) {
		return super.isCellEditable( rowIndex, columnIndex);
	}*/

//	public Class<?> getColumnClass(int columnIndex) {
//		if (columnIndex == 0) {
//			return Integer.class;
//		}
//		return String.class;
//	}

	public String getColumnName(int column) {
		return header[column];
	}

	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
		fireTableDataChanged();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		if (employeeList.isEmpty()) {
			return null;
		}
		Employee employee = employeeList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return rowIndex + 1;
		case 1:
			return employee.getEmpNumber();
		case 2:
			return employee.getPassword();
		case 3:
			return employee.getAddress();
		case 4:
			return employee.getAllowance();
		case 5:
			return employee.getBirthday();
		case 6:
			return employee.getCurrentDepartment();
		case 7:
			return employee.getCurrentLocation();
		case 8:
			return employee.getCurrentProject();
		case 9:
			return employee.getEmail();
		case 10:
			return employee.getPhone();
		case 11:
			return employee.getRoles();
		case 12:
			return employee.getWorkExp();
		case 13:
			return employee.getEmpName();
		}
		return null;
	}

	public void addEmployee(Employee employee) throws IOException,
			ClassNotFoundException, SQLException {
		EmployeeManageDao.insert(employee);
		//employeeList.add(employee);
		fireTableDataChanged();
	}

	public void editEmployee(int row, Employee employee) {
		employeeList.set(row, employee);
		fireTableDataChanged();
	}

	public void deleteEmployee(Employee employee ) throws IOException, ClassNotFoundException, SQLException {
		EmployeeManageDao.Delete(employee);
		//employeeList.remove(row);
		fireTableDataChanged();
	}
	
	public void loadUser() {
	}
}
